package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.CourierService;
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

	private static final long serialVersionUID = 1L;
	private Courier courier=new Courier();
	//模型驱动
	@Override
	public Courier getModel() {
		return courier;
	}
	@Autowired
	private CourierService courierService;
	/**
	 * 添加
	 * 
	 */
	@Action(value="courier_save",results= {@Result(name="success",location="./pages/base/courier.html",type="redirect"),
	@Result(name="error",location="./unauthorized.html",type="redirect")	
		})
	public String save() {
		try {
			courierService.save(courier);
		} catch (Exception e) {
			return "error";
		}
		return SUCCESS;
	}
	/**
	 * 没有条件的分页查询
	 */
	private int page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	@Action(value="courier_pageQuery",results= {@Result(name="success",type="json")})
	public String pageQuery() {
//		//调用service层,查询返回数据
//		Pageable pageable=new PageRequest(page-1, rows);
//		Page<Courier> pageData= courierService.findPageData(pageable);
//		//返回客户端数据
//		Map<String, Object> map=new HashMap<String ,Object>();
//		map.put("total", pageData.getTotalElements());
//		map.put("rows", pageData.getContent());
//		ActionContext.getContext().getValueStack().push(map);
		/**
		 * 有条件查询
		 * 
		 */
		//调用service层,查询返回数据
		Pageable pageable=new PageRequest(page-1, rows);
		//组织查询条件
		Specification<Courier> specification=new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				 Predicate predicate =null;
				List<Predicate> list=new ArrayList<Predicate>();
				//快递员工号
				if (StringUtils.isNotBlank(courier.getCourierNum())) {
					Predicate p = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					list.add(p);
				}
				//所属单位
				if (StringUtils.isNotBlank(courier.getCompany())) {
					Predicate p = cb.like(root.get("company").as(String.class), "%"+courier.getCompany()+"%");
					list.add(p);
				}
				//快递员类型
				if (StringUtils.isNotBlank(courier.getType())) {
					Predicate p = cb.like(root.get("type").as(String.class),"%"+ courier.getType()+"%");
					list.add(p);
				}
				//收派标准
				if (courier.getStandard()!=null&&courier.getStandard().getId()!=null) {
					//联合查询
					//创建联合查询对象
					Join<Courier, Standard> join = root.join("standard",JoinType.INNER);
					Predicate p = cb.equal(join.get("id").as(Integer.class), courier.getStandard().getId());
					list.add(p);
				}
				if (list!=null&&list.size()>0) {
					Predicate [] p=new Predicate[list.size()];
					predicate=cb.and(list.toArray(p));
				}
				return predicate;
			}
		};
		Page<Courier> pageData= courierService.findPageDataCondition(specification, pageable);                  
		//返回客户端数据
		Map<String, Object> map=new HashMap<String ,Object>();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		ActionContext.getContext().getValueStack().push(map);
		
		return SUCCESS;
	}

	/**
	 * 作废快递员
	 * 
	 */
	private String ids;
	public void setIds(String ids) {
		System.out.println(ids);
		this.ids = ids;
	}
	@Action(value="courier_deleteCourier",results= {@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String deleteCourier() {
		String[] idArray = ids.split(",");
		//调用业务层,删除
		System.out.println("000000");
		courierService.deleteCourier(idArray);
		System.out.println("111111");
		return SUCCESS;
		
	}
	/**
	 * 查询没有关联的快递员
	 */
	@Action(value="courier_findnoassociation",results= {@Result(name="success",type="json")})
	public String findnoassociation() {
		List<Courier> couriers=courierService.findNoAssociation();
		//将查询数据压入栈
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
		
	}
	
	/**
	 * 查询当前id对应的快递员
	 */
	private String fixedAreaId;
	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}
	@Action(value="courier_findAssociationCourie",results= {@Result(name="success",type="json")})
	public String findAssociationCourie() {
		List<Courier> list=courierService.findAssociationCourier(fixedAreaId);
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

}

















